package com.hzdp.netty.aio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

import org.apache.log4j.Logger;

public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncTimeServerHandler> {

	protected static Logger logger = Logger.getLogger(AcceptCompletionHandler.class);

	@Override
	public void completed(AsynchronousSocketChannel result, AsyncTimeServerHandler attachment) {
		// accept 可以异步接受其他客户端
		attachment.asynchronousServerSocketChannel.accept(attachment, this);

		ByteBuffer buffer = ByteBuffer.allocate(1024);

		result.read(buffer, buffer, new ReadCompletionHandler(result));
	}

	@Override
	public void failed(Throwable exc, AsyncTimeServerHandler attachment) {
		exc.printStackTrace();
		attachment.countDownLatch.countDown();
	}

}
